# from flask.ext.restful import Api,Resource
from app import Models,app
from app.Models import db
from werkzeug.security import generate_password_hash, check_password_hash
import datetime


# api = Api(app)

# 将明密码转化为hash码
def enPassWord(password):
    # 返回转换的hash码
    return generate_password_hash(password, method='pbkdf2:sha1', salt_length=8)


# 第一参数是从数据查询出来的hash值，第二参数是需要检验的密码
def checkPassWord(enpassword,password):
    # 如果匹配返回true
    return check_password_hash(enpassword,password)


# 用户
class user():
    def __init__(self, username=None, password=None, group=None,
                 last_user=None, changes=None, mid=None,
                 page=1, total=5, search=None):
        self.username = username
        self.password = password
        self.group = group
        self.last_user = last_user
        self.changes = changes
        self.mid = mid
        self.page = page
        self.total = total
        self.search = search

    # 全部
    def All(self):
        if self.search is None:
            users = Models.User_users.query.paginate(
                self.page, per_page=self.total, error_out=True)
        else:
            users = Models.User_users.query.filter(Models.User_users.username == self.search).paginate(
            self.page, per_page=self.total, error_out=True)
        return users
    # 增加
    def Increase(self):
        # 查询用户名是否存在
        if user.Check(self)[0] is True:
            message = '用户名存在'
            return False, message
        check_password = enPassWord(self.password)
        aa = Models.User_users(username=self.username, password=check_password, group=self.group)
        db.session.add(aa)
        db.session.commit()
        cc = user.Check(self)[1]
        return True,cc


    # 删除
    def Delete(self):
        if self.username is None:
            dd = Models.User_users.query.filter(Models.User_users.mid == self.mid).first()
        else:
            dd = Models.User_users.query.filter(Models.User_users.username == self.username).first()
        if str(dd) == 'None':
            return False
        else:
            db.session.delete(dd)
            db.session.commit()
            return True

    # 修改
    def Change(self):
        cc = Models.User_users.query.filter(Models.User_users.username==self.username).first()
        if str(cc) == 'None':
            return False
        else:
            if self.changes == 'password':
                cc.password = self.last_user
            elif self.changes == 'group':
                cc.group = self.last_user
            cc.last_login = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            db.session.commit()
            return True
    # 查询
    # number=1  登录验证
    def Check(self, number=None):
        print("user:",self.username)
        if self.username is None:
            bb = Models.User_users.query.filter(Models.User_users.mid==self.mid).first()
        else:
            bb = Models.User_users.query.filter(Models.User_users.username == self.username).first()
        if str(bb) == 'None':
            message = '账号不存在'
            return False, message
        else:
            if number == 1:
                if checkPassWord(bb.password, self.password) is False:
                    message = '密码错误'
                    return False, message
            cc = {'mid': bb.mid, 'username': bb.username, 'last_login': bb.last_login.strftime("%Y-%m-%d %H:%M:%S"), 'date_joined': bb.date_joined.strftime("%Y-%m-%d %H:%M:%S"), 'group': bb.group}
            return True, cc
# 个人中心
class center_u():
    def __init__(self, mid=None, user=None, phone=None, nickname=None, qq_number=None):
        self.mid = mid
        self.user = user
        self.phone = phone
        self.nickname = nickname
        self.qq_number = qq_number
    # 增加
    def Increase(self):
        aa = Models.User_center(mid=self.mid, user=self.user, nickname=self.user)
        db.session.add(aa)
        db.session.commit()
        return True
    # 修改
    def Change(self):
        if center_u.Check(self, number=1) is None :
            mb = '昵称重复'
            return False, mb
        elif center_u.Check(self, number=2) is None :
            mb = '手机号重复'
            return False, mb
        cc = Models.User_center.query.filter(Models.User_center.mid==self.mid).first()
        if self.phone != '':
            cc.phone = self.phone
        if self.qq_number != '':
            cc.qq_number = self.qq_number
        if self.nickname != '':
            cc.nickname = self.nickname
        db.session.commit()
        return True
    # 查询
    # number = 1 查昵称
    # number = 2 查手机号
    def Check(self, number=None):
        if number == 1:
            bb = Models.User_center.query.filter(Models.User_center.nickname == self.nickname).first()
        elif number == 2:
            bb = Models.User_center.query.filter(Models.User_center.phone == self.phone).first()
        else:
            bb = Models.User_center.query.filter(Models.User_center.mid==self.mid).first()
        if str(bb) == 'None':
            cc = 'None'
        else:
            cc = {'mid': bb.mid, 'user': bb.user,'phone':bb.phone, 'nickname':bb.nickname,
            'qq_number':bb.qq_number,'last_login': bb.last_login.strftime("%Y-%m-%d %H:%M:%S")}
        return cc

    # 删除
    def Delete(self):
        dd = Models.User_center.query.filter(Models.User_center.mid == self.mid).first()
        if str(dd) == 'None':
            return False
        else:
            db.session.delete(dd)
            db.session.commit()
            return True

# 用户组
class group():
    def __init__(self, name=None, id=None, last_name=None,
                 search=None, page=1, total=4):
        self.name = name
        self.id = id
        self.last_name = last_name
        self.page = page
        self.total = total
        self.search = search

    # 全部
    def All(self):
        if self.search is None:
            users = Models.User_groups.query.paginate(
                self.page, per_page=self.total, error_out=True)
        else:
            users = Models.User_groups.query.filter(Models.User_groups.user == self.search).paginate(
                self.page, per_page=self.total, error_out=True)
        return users
    # 增加
    def Increase(self):
        if group.Check(self)[0] is not False:
            return False,"名称已存在"
        aa = Models.User_groups(name=self.name)
        db.session.add(aa)
        db.session.commit()
        return True

    # 删除
    def Delete(self):
        if self.name is None:
            dd = Models.User_groups.query.filter(Models.User_groups.id == self.id).first()
        else:
            dd = Models.User_groups.query.filter(Models.User_groups.name == self.name).first()
        if str(dd) == 'None':
            return False, "名称不存在"
        else:
            db.session.delete(dd)
            db.session.commit()
            return True

    # 修改
    def Change(self):
        if group.Check(self, number=1)[0] is not False:
            return False,"名称已存在"
        if self.name is None:
            cc = Models.User_groups.query.filter(Models.User_groups.id == self.id).first()
        else:
            cc = Models.User_groups.query.filter(Models.User_groups.name == self.name).first()
        if str(cc) == 'None':
            return False, "名称不存在"
        else:
            cc.name = self.last_name
            db.session.commit()
            return True
    # 查询
    # number = 1 修改查询是否存在
    def Check(self, number=None):
        if number == 1:
            bb = Models.User_groups.query.filter(Models.User_groups.name == self.last_name).first()
        else:
            if self.name is None:
                bb = Models.User_groups.query.filter(Models.User_groups.id == self.id).first()
            else:
                bb = Models.User_groups.query.filter(Models.User_groups.name==self.name).first()
        if str(bb) == 'None':
            return False, "名称不存在"
        else:
            cc = {"id":bb.id, "name":bb.name}
            return True,cc

# 图片管理
class Images_page():
    def __init__(self,id=None, name=None, Route=None, position=None,
                 last_name=None, search=None, page=None, total=None):
        self.name = name
        self.Route = Route
        self.position = position
        self.id = id
        self.last_name = last_name
        self.search =search
        self.page = page
        self.total = total
    # 全部
    def All(self):
        if self.search is None and self.position is None:
            users = Models.Photo_index.query.paginate(
                self.page, per_page=self.total, error_out=True)
        elif self.position is None:
            print(1)
            users = Models.Photo_index.query.filter(Models.Photo_index.name == self.search).paginate(
                self.page, per_page=self.total, error_out=True)
        else:
            users = Models.Photo_index.query.filter(Models.Photo_index.position==self.position).all()
        return users
    # 增加
    def Increase(self):
        if Images_page.Check(self)[0] is not False:
            return False,"名称已存在"
        # bb = Models.Photo_index.filter(Models.Photo_index.name==self.name,Models.Photo_index.Route==self.Route, Models.Photo_index.position==self.position).first()
        aa = Models.Photo_index(name=self.name, Route=self.Route, position=self.position)
        db.session.add(aa)
        db.session.commit()
        return True
    # 删除
    def Delete(self):
        if self.name is None:
            dd = Models.Photo_index.query.filter(Models.Photo_index.id == self.id).first()
        else:
            dd = Models.Photo_index.query.filter(Models.Photo_index.name == self.name).first()
        if str(dd) == 'None':
            return False, "名称不存在"
        else:
            db.session.delete(dd)
            db.session.commit()
            return True
    # 修改
    def Change(self):
        if self.name is None:
            cc = Models.Photo_index.query.filter(Models.Photo_index.id == self.id).first()
        else:
            cc = Models.Photo_index.query.filter(Models.Photo_index.name == self.name).first()
        if str(cc) == 'None':
            return False, "名称不存在"
        else:
            if self.Route is None:
                cc.name = self.last_name
            else:
                cc.Route = self.Route
            db.session.commit()
            return True
    # 查询
    def Check(self):
        if self.name is None:
            bb = Models.Photo_index.query.filter(Models.Photo_index.id == self.id).first()
        else:
            bb = Models.Photo_index.query.filter(Models.Photo_index.name==self.name).first()
        if str(bb) == 'None':
            return False, "名称不存在"
        else:
            cc = {"id": bb.id, "name": bb.name,
                  "last_login": bb.last_login.strftime("%Y-%m-%d %H:%M:%S"),
                  "Route": bb.Route, "position": bb.position}
            return True,cc

# 信息展示
class Work_station():
    def __init__(self, id=None,name=None,singer=None,Lyrics=None,
                 Composer=None,Arranger=None,Mix=None,Lyric_ci=None,
                 Original_song=None,Type=None,Address=None, page=1,total=4,
                 search=None, time=None,photo=None,give_up=None,give_down=None):
        self.id = id
        self.name = name
        self.singer = singer
        self.Lyrics = Lyrics
        self.Composer = Composer
        self.Arranger = Arranger
        self.Mix = Mix
        self.Lyric_ci = Lyric_ci
        self.Original_song = Original_song
        self.Type = Type
        self.Address = Address
        self.page = page
        self.total = total
        self.search =search
        self.time = time
        self.photo = photo
        self.give_up = give_up
        self.give_down = give_down
        if self.time is None:
            self.time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    # 全部
    def All(self):
        if self.search is None:
            users = Models.Display_works.query.filter(
                Models.Display_works.Type == self.Type).paginate(
                self.page, per_page=self.total, error_out=True)
        else:
            users = Models.Display_works.query.filter(
                Models.Display_works.name == self.search,
                Models.Display_works.Type == self.Type).paginate(
                self.page, per_page=self.total, error_out=True)
        return users
    # 增加
    def Increase(self):
        aa = Models.Display_works(name=self.name,singer=self.singer,Lyrics=self.Lyrics,
                                Composer=self.Composer,Arranger=self.Arranger,Mix=self.Mix,
                                Lyric_ci=self.Lyric_ci,Original_song=self.Original_song,
                                Type=self.Type,Address=self.Address,time=self.time,photo=self.photo,
                                  give_up=0,give_down=0)
        db.session.add(aa)
        db.session.commit()
        return True
    # 删除
    def Delete(self):
        if self.name is None:
            dd = Models.Display_works.query.filter(Models.Display_works.id == self.id).first()
        else:
            dd = Models.Display_works.query.filter(Models.Display_works.name == self.name).first()
        if str(dd) == 'None':
            return False, "名称不存在"
        else:
            db.session.delete(dd)
            db.session.commit()
            return True
    # 修改
    def Change(self):
        print("id:",self.id,"photo:",self.photo)
        cc = Models.Display_works.query.filter(Models.Display_works.id == self.id).first()
        if str(cc) == 'None':
            return False, "名称不存在"
        else:
            if self.name is not None:
                cc.name = self.name
            if self.singer is not None:
                cc.singer = self.singer
            if self.Lyrics is not None:
                cc.Lyrics = self.Lyrics
            if self.Composer is not None:
                cc.Composer = self.Composer
            if self.Arranger is not None:
                cc.Arranger = self.Arranger
            if self.Mix is not None:
                cc.Mix = self.Mix
            if self.Lyric_ci is not None:
                cc.Lyric_ci = self.Lyric_ci
            if self.Original_song is not None:
                cc.Original_song = self.Original_song
            if self.Type is not None:
                cc.Type = self.Type
            if self.Address is not None:
                cc.Address = self.Address
            if self.give_down is not None:
                cc.give_down = self.give_down
            if self.give_up is not None:
                cc.give_up = self.give_up
            if self.photo is not None:
                cc.photo = self.photo
            db.session.commit()
            return True
    # 查询
    def Check(self):
        if self.Type is not None:
            bb = Models.Display_works.query.filter(Models.Display_works.Type == self.Type).order_by(Models.Display_works.time.desc()).first()
            if str(bb) == 'None':
                return False
            else:
                return bb.name
        else:
            bb = Models.Display_works.query.filter(Models.Display_works.id == self.id).first()
        if str(bb) == 'None':
            return False, "名称不存在"
        else:
            cc = {"id": bb.id, "name": bb.name,"singer": bb.singer,"Lyrics": bb.Lyrics,
                  "Composer": bb.Composer, "Arranger": bb.Arranger,
                  "Mix": bb.Mix,"Lyric_ci": bb.Lyric_ci,
                  "Original_song": bb.Original_song,"Type": bb.Type,
                  "Address": bb.Address,"date_joined": bb.date_joined,
                  "last_login": bb.last_login,"time":bb.time,"photo":bb.photo,
                  "give_up":bb.give_up,"give_down":bb.give_down}
            print(cc)
            return True,cc

# 点赞
class Thumb_sp():
    def __init__(self,id=None,name_id=None,Type=None,mid=None,time=None):
        self.id = id
        self.name_id = name_id
        self.Type = Type
        self.mid = mid
        self.time=time
    def all_count(self,count):
        if count == 1:
            cc = Models.thumbs_up.query.filter(Models.thumbs_up.name_id == self.name_id,
                                           Models.thumbs_up.Type == self.Type).count()
        else:
            cc = Models.thumbs_up.query.filter(Models.thumbs_up.mid == self.mid,
                                               Models.thumbs_up.Type == self.Type,
                                               Models.thumbs_up.date_joined == self.time).count()
        return cc
    def Increase(self):
        aa = Models.thumbs_up(name_id=self.name_id, Type=self.Type, mid=self.mid)
        db.session.add(aa)
        db.session.commit()
        am = Thumb_sp.all_count(self,count=1)
        print(am,"am")
        Work_station(id=self.name_id,give_up=am).Change()
        return True

    def Check(self):
        bb = Models.thumbs_up.query.filter(Models.thumbs_up.name_id == self.name_id,
                                           Models.thumbs_up.Type == self.Type,
                                           Models.thumbs_up.mid == self.mid,
                                           Models.thumbs_up.date_joined ==self.time).first()
        if str(bb) == 'None':
            return False
        else:
            cc = {"id": bb.id, "name_id": bb.name_id,"Type": bb.Type,"mid": bb.mid,
                  "date_joined": bb.date_joined}
            return cc

# 脚本
class Implement():
    def __init__(self,id=None,name=None, Route=None, page=1,total=4):
        self.id = id
        self.name = name
        self.Route = Route
        self.page = page
        self.total =total
    def All(self):
        users = Models.Script_fu.query.paginate(
            self.page, per_page=self.total, error_out=True)
        return users
    # 增加
    def Increase(self):
        aa = Models.Script_fu(name=self.name, Route=self.Route)
        db.session.add(aa)
        db.session.commit()
        return True
    # 查询
    def Check(self):
        bb = Models.Script_fu.query.filter(Models.Script_fu.id == self.id).first()
        if str(bb) == 'None':
            return False, "名称不存在"
        else:
            cc = {"id": bb.id, "name": bb.name,
                  "Route": bb.Route,
                  "date_joined": bb.date_joined, "last_login": bb.last_login}
            return True,cc

    # 删除
    def Delete(self):
        dd = Models.Script_fu.query.filter(Models.Script_fu.id == self.id).first()
        if str(dd) == 'None':
            return False, "名称不存在"
        else:
            db.session.delete(dd)
            db.session.commit()
            return True

    # 修改
    def Change(self):
        cc = Models.Script_fu.query.filter(Models.Script_fu.id == self.id).first()
        if str(cc) == 'None':
            return False, "名称不存在"
        else:
            if self.name is not None:
                cc.name = self.name
            if self.Route is not None:
                cc.Route = self.Route
            db.session.commit()
            return True
class BB():
    def __init__(self,page=1,total=4):
        self.page = page
        self.total = total
    def AA(self):
        pass

